package cn.tedu.sp06.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.web.util.JsonResult;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import sun.security.krb5.internal.Krb5;

import javax.ws.rs.GET;
import java.util.List;

@RestController
@Slf4j
public class RibbonController {

    @Autowired
    private RestTemplate restTemplate;
    //获取商品列表
    @HystrixCommand(fallbackMethod = "getItemsFB")
    @GetMapping("/item-service/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        return restTemplate.getForObject("http://item-service/{1}",JsonResult.class,
                                             orderId);
    }


    //减少商品库存
    @HystrixCommand(fallbackMethod = "decreaseNumberFB")
    @PostMapping("/item-service/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        return restTemplate.postForObject("http://item-service/decreaseNumber", items, JsonResult.class);
    }
    @HystrixCommand(fallbackMethod = "getUserFB")
    @GetMapping("/user-service/{userId}")
    public JsonResult<User> getUser(@PathVariable Integer userId){
        return restTemplate.getForObject("http://USER-SERVICE/{1}", JsonResult.class,userId);
    }
    @HystrixCommand(fallbackMethod = "addScoreFB")
    @GetMapping("/user-service/{userId}/score")
    public JsonResult<?> addScore(@PathVariable Integer userId,Integer score){
        return restTemplate.getForObject("http://USER-SERVICE/{1}/score?score={2}",JsonResult.class,userId,score);
    }
    @HystrixCommand(fallbackMethod = "getOrderFB")
    @GetMapping("/order-service/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return restTemplate.getForObject("http://ORDER-SERVICE/{1}", JsonResult.class,orderId);
    }
    @HystrixCommand(fallbackMethod = "addOrderFB")
    @GetMapping("/order-service/")
    public JsonResult<?> addOrder(){
        return restTemplate.getForObject("http://ORDER-SERVICE/", JsonResult.class);
    }
    //===============================================================================//





    public JsonResult<List<Item>> getItemsFB(@PathVariable String orderId){
        return JsonResult.err().msg("获取商品列表失败");
    }

    public JsonResult<?> decreaseNumberFB(@RequestBody List<Item> items){
        return JsonResult.err().msg("减少商品列表失败");
    }

    public JsonResult<User> getUserFB(@PathVariable Integer userId){
        return JsonResult.err().msg("获取用户信息失败");
    }

    public JsonResult<?> addScoreFB(@PathVariable Integer userId,Integer score){
        return JsonResult.err().msg("增加积分失败");
    }

    public JsonResult<Order> getOrderFB(@PathVariable String orderId){
        return JsonResult.err().msg("获取订单失败");
    }

    public JsonResult<?> addOrderFB(){
        return JsonResult.err().msg("保存订单失败");
    }


}
